package com.buss.product.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.buss.core.persistence.BaseDao;
import com.buss.core.persistence.ResultList;
import com.buss.product.dao.GoodsDetailDao;
import com.buss.product.model.GoodsDetail;

@Repository
public class GoodsDetailDaoImpl implements GoodsDetailDao {

	@Autowired
	private BaseDao<GoodsDetail> baseDao;
	
	@Override
	public ResultList<GoodsDetail> queryGoodsDetail(Long goodsId) {
		String sql = "select detail from GoodsDetail detail where detail.goods.id = :goodsId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("goodsId", goodsId);
		return baseDao.queryHsql(sql, params, null);
	}

	@Override
	public GoodsDetail saveGoodsDetail(GoodsDetail detail) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void editGoodsDetail(GoodsDetail detail) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void removeGoodsDetail(Long id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void removeGoodsDetailByGoodsId(Long id) {
		String sql = "delete from GoodsDetail detail where detail.goods.id = :goodsId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("goodsId", id);
		baseDao.excuteHsql(sql, params);
		
	}

	@Override
	public void removeGoodsDetailByNotIds(Long goodsId,Long[] ids) {
		String sql = "delete from GoodsDetail detail where detail.goods.id = :goodsId ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("goodsId", goodsId);
		if(ids != null && ids.length > 0){
			sql += " and detail.id not in (:ids)";
			params.put("ids", ids);
		}		
		baseDao.excuteHsql(sql, params);
	}

	public ResultList<GoodsDetail> queryGoodsDetailByBarcode(String companyId,String branchId,String barCode){
		String sql = "select detail from GoodsDetail detail where detail.goods.category.companyId = :companyId " +
				"and (detail.goods.branchId = '0' or detail.goods.branchId like :branchId) " +
				"and detail.barCode = :barCode ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("companyId", companyId);
		params.put("branchId", "," + branchId + ",");
		params.put("barCode", barCode);
		return baseDao.queryHsql(sql, params, null);
	}
	
}
